package cn.mw.cmdb.template.entity;

import cn.mw.cmdb.entity.PropertyTypeInt;
import cn.mw.cmdb.entity.PropertyTypeList;
import cn.mw.cmdb.entity.PropertyTypeString;
import cn.mw.cmdb.template.constant.MwTemplateConfigurationConstant;
import cn.mw.cmdb.template.entity.convert.MwTemplateZabbixConvertDto;
import cn.mw.components.mongodb.anno.AutoId;
import cn.mw.components.mongodb.anno.CMDBProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gengjb
 * @description 模板编辑实体
 * @date 2024/7/26 10:54
 */
@Getter
@Setter
@Schema(name = "MwTemplateEditorDto", description = "模板编辑实体")
@Document("mw_cmdbmd_template_configuration")
public class MwTemplateConfigurationDto implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "模板配置ID")
    @CMDBProperty(searchType = PropertyTypeString.ID, searchPrefix = "_", isShow = true)
    @AutoId
    private String id;

    @Schema(description = "模板ID")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String templateId;

    @Schema(description = "服务器ID")
    @CMDBProperty(searchType = PropertyTypeInt.ID ,keywordSearch = true)
    private Integer serverId;

    @Schema(description = "模板名称")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String templateName;

    @Schema(description = "主机群组")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String group;

    @Schema(description = "主机")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String host;

    @Schema(description = "应用集")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String application;

    @Schema(description = "监控项")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String monitorItems;

    @Schema(description = "触发器")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String trigger;

    @Schema(description = "图形")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String graph;

    @Schema(description = "仪表盘")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String dashboards;

    @Schema(description = "自动发现")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String discoveries;

    @Schema(description = "WEB监测")
    @CMDBProperty(searchType = PropertyTypeString.ID ,keywordSearch = true)
    private String webMonitoring;

    @Schema(description = "状态")
    @CMDBProperty(searchType = PropertyTypeInt.ID ,keywordSearch = true)
    private Integer status;

    @Schema(description = "链接模板")
    @CMDBProperty(searchType = PropertyTypeList.ID ,keywordSearch = true)
    private List linkTemplate;

    @Schema(description = "链接模板ID")
    @Transient
    private List<String> linkTemplateIds;

    @Transient
    public void extractFrom(MwTemplateZabbixConvertDto zabbixConvertDto,Integer serverId){
        this.status = 1;
        this.serverId = serverId;
        this.templateId = zabbixConvertDto.getTemplateid();
        this.templateName = zabbixConvertDto.getName();
        this.group = zabbixConvertDto.getGroups().get(0).getGroupid();
        this.host = zabbixConvertDto.getHosts();
        if(zabbixConvertDto.getApplications() != null){
            this.application = zabbixConvertDto.getApplications();
        }else{
            if(CollectionUtils.isNotEmpty(zabbixConvertDto.getTags())){
                List<MwTemplateZabbixConvertDto.TagDto> tagDtos = zabbixConvertDto.getTags().stream().filter(item -> item.getTag().equals(MwTemplateConfigurationConstant.APPLICATION)).collect(Collectors.toList());
                if(CollectionUtils.isEmpty(tagDtos)){
                    this.application = String.valueOf(0);
                }else{
                    this.application = String.valueOf(zabbixConvertDto.getTags().size());
                }
            }else {
                this.application = String.valueOf(0);
            }
        }
        this.monitorItems = zabbixConvertDto.getItems();
        this.trigger = zabbixConvertDto.getTriggers();
        this.graph = zabbixConvertDto.getGraphs();
        this.dashboards = zabbixConvertDto.getDashboards();
        this.discoveries = zabbixConvertDto.getDiscoveries();
        this.webMonitoring = zabbixConvertDto.getHttpTests();
        List<MwTemplateZabbixConvertDto.ChildTemplatesDto> templates = zabbixConvertDto.getTemplates();
        List<MwTemplateZabbixConvertDto.ParentTemplatesDto> parentTemplates = zabbixConvertDto.getParentTemplates();
        this.linkTemplateIds = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(templates)){
            linkTemplateIds.addAll(templates.stream().map(s->s.getTemplateid()).collect(Collectors.toList()));
        }
        if(CollectionUtils.isNotEmpty(parentTemplates)){
            linkTemplateIds.addAll(parentTemplates.stream().map(s->s.getTemplateid()).collect(Collectors.toList()));
        }
    }
}
